package cn.pconline.autoclub.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.gelivable.auth.entity.GeliSession;
import org.gelivable.auth.entity.GeliUser;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.UpgradeGift;
import cn.pconline.autoclub.entity.UpgradeGiftReward;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.util.Pager;

/**
 * Service-车友会升星礼品奖励
 * @author zhoutianhuai
 */
public class UpgradeGiftRewardService {

    @Autowired
    private GeliDao geliDao;
    @Autowired
    private GeliOrm geliOrm;
    @Autowired
    private PagerService pagerService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private UpgradeGiftService upgradeGiftService; 
    private static final Logger log = LoggerFactory.getLogger(UpgradeGiftRewardService.class);
    
    public UpgradeGiftReward createUpgradeGiftReward(long userId, long clubId, UpgradeGift gift, String trueName, String address, String postcode, String phone,int num) {
    	int curNum = gift.getCurNum();
        if (curNum < num) {
            return null;
        }
        UpgradeGiftReward upgradeGiftReward = new UpgradeGiftReward();
        upgradeGiftReward.setGiftId(gift.getGiftId());
        upgradeGiftReward.setUserId(userId);
        upgradeGiftReward.setValuePoint(gift.getValuePoint()*num);
        upgradeGiftReward.setTrueName(trueName);
        upgradeGiftReward.setAddress(address);
        upgradeGiftReward.setPostcode(postcode);
        upgradeGiftReward.setPhone(phone);
        upgradeGiftReward.setNum(num);
        upgradeGiftReward.setStatus(UpgradeGiftReward.STATUS_NOSEND);
        
        Date cur = new Date();
        upgradeGiftReward.setCreateAt(cur);
        upgradeGiftReward.setUpdateAt(cur);
        long id = geliDao.create(upgradeGiftReward);
        if (id > 0) {
        	 if (gift.getCurNum() > 0) {
                 gift.setCurNum(gift.getCurNum() - num);
             }
        	 upgradeGiftService.updateUpgradeGift(gift);
            return upgradeGiftReward;
        }
        return null;
    }

    public int updateUpgradeGiftReward(UpgradeGiftReward upgradeGiftReward) {
    	upgradeGiftReward.setUpdateAt(new Date());
        GeliUser u = GeliSession.getCurrentUser();
        long uid = 0;
        if (u != null) {
            uid = u.getUserId();
        }
        upgradeGiftReward.setAuditBy(uid);
        return geliDao.update(upgradeGiftReward);
    }



    public UpgradeGiftReward findUpgradeGiftReward(long id) {
        try {
            return geliDao.find(UpgradeGiftReward.class, id);
        } catch (DataAccessException ex) {
            return null;
        }
    }

    
    public Pager<UpgradeGiftReward> pageUpgradeGiftReward(long userId, long clubId, long giftId, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select reward_id from ").appendSql(geliOrm.getTableName(UpgradeGiftReward.class)).appendSql(" where 1=1");
        if (userId > 0) {
            sql.appendSql(" and user_id = ").appendValue(userId);
        }
        if (clubId > 0) {
            sql.appendSql(" and club_id = ").appendValue(clubId);
        }
        if (giftId > 0) {
            sql.appendSql(" and gift_id = ").appendValue(giftId);
        }
        sql.appendSql(" order by create_at desc");//倒序
        return pagerService.pager(UpgradeGiftReward.class, sql, pageNo, pageSize);
    }
    
    public Pager<UpgradeGiftReward> pageUpgradeGiftRewardWithGift(long userId, long clubId, long giftId, String truename,String clubName, int status, String beginDate, String endDate, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select reward_id from ").appendSql(geliOrm.getTableName(UpgradeGiftReward.class)).appendSql(" ac_upGrade left join ");
        sql.appendSql(geliOrm.getTableName(Club.class)).appendSql(" ac_club on ac_upGrade.club_id = ac_club.club_id where 1=1 ");
        if (userId > 0) {
            sql.appendSql(" and ac_upGrade.user_id = ").appendValue(userId);
        }
        if (giftId > 0) {
            sql.appendSql(" and ac_upGrade.gift_id = ").appendValue(giftId);
        }
        if (clubId > 0) {
            sql.appendSql(" and ac_upGrade.club_id = ").appendValue(clubId);
        }
        if (StringUtils.isNotBlank(truename)) {
            sql.appendSql(" and ac_upGrade.truename like ").appendValue("%" + truename + "%");
        }
        if(StringUtils.isNotBlank( clubName )){
        	sql.appendSql(" and ac_club.name like ").appendValue("%" + clubName + "%");
        }
        if (status > UpgradeGiftReward.STATUS_NOSELECT) {
            sql.appendSql(" and ac_upGrade.status=").appendValue(status);
        }
        if (StringUtils.isNotBlank(beginDate)) {
            sql.appendSql(" and ac_upGrade.create_at>=").appendValue(beginDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            sql.appendSql(" and ac_upGrade.create_at <=").appendValue(endDate);
        }
        sql.appendSql(" order by ac_upGrade.create_at desc");
        Pager<UpgradeGiftReward> pager = pagerService.pager(UpgradeGiftReward.class, sql, pageNo, pageSize);
        List<UpgradeGiftReward> list = pager.getResultList();
        if (list != null && list.size() > 0) {
            long[] gIds = new long[list.size()];
            for (int i = 0; i < list.size(); i++) {
            	UpgradeGiftReward upgradeGiftReward = list.get(i);
                gIds[i] = upgradeGiftReward.getGiftId();
                //设置用户对象
                User user = userService.findUser(upgradeGiftReward.getUserId());
                upgradeGiftReward.setUser(user);
                //设置车友会对象
                Club club = clubService.getClub(upgradeGiftReward.getClubId());
                upgradeGiftReward.setClub(club);
            }
            List<UpgradeGift> upgradeGifts = geliDao.list(UpgradeGift.class, gIds);
            if (upgradeGifts != null) {
                for (int i = 0; i < upgradeGifts.size(); i++) {
                    list.get(i).setUpgradeGift(upgradeGifts.get(i));
                }
            }
        }
        return pager;
    }
}
